/**
 * 
 */
package edu.pku.sei.modeler.figures;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.LineBorder;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.draw2d.XYLayout;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;

import edu.pku.notation.runtime.draw2d.RoundedRectangleFigure;
import edu.pku.notation.runtime.draw2d.TextFigure;
import edu.pku.sei.modeler.uml.ResourceFactory;

/**
 * @author YanYan
 *
 */
public class ComponentFigure extends CommonFigure {

	public IFigure root;
	
	public IFigure body;
	
	public IFigure getBody() {
		return body;
	}

	private TextFigure componentLable = null;
	
	public ComponentFigure(final Label header){
		
		super();

//		ToolbarLayout layout = new ToolbarLayout();
		XYLayout layout = new XYLayout();
		setLayoutManager(layout);
		setForegroundColor(ColorConstants.blue);
		setBorder(new LineBorder(1));
		
		root = new RoundedRectangleFigure();
		ToolbarLayout rootLayout = new ToolbarLayout();
		rootLayout.setVertical(true);
		root.setLayoutManager(rootLayout);
		root.setForegroundColor(ColorConstants.black);
		
		if (header.getText().length() > 0) {
			root.add(header);
		}
		
		componentLable = new TextFigure();
		componentLable.setOpaque(false);
		componentLable.setFont(ResourceFactory.getHeadFont());
		componentLable.setForegroundColor(ColorConstants.black);
		root.add(componentLable);
		
		body = new RoundedRectangleFigure();
		XYLayout xyL = new XYLayout();
		body.setLayoutManager(xyL);
		body.setOpaque(true);
		//body.setBorder(new LineBorder(1));
		root.add(body);
		
		add(root);
				
	}
	
	public void changeLineColor(Color color) {
		this.setForegroundColor(color);
		root.setForegroundColor(color);
//		body.setForegroundColor(color);
		((LineBorder)this.getBorder()).setColor(color);
	}
	
	public void paint(Graphics graphics){
		
		super.paint(graphics);
		
		graphics.pushState();
		graphics.setAntialias(SWT.ON);
		Rectangle figureArea = getBounds();
		
//		graphics.setForegroundColor(ColorConstants.buttonDarkest);
		
		int width = figureArea.width;		
		int relativeX = figureArea.x+width;
		int relativeY = figureArea.y;
		
		int relativeXofLeft = 25;
		int relativeXofRight = 7;
		int relativeYofTop = 7;
		int relativeYofBottom = 32;
		
		// this Upper means the upper small rectangle
		int relativeXofUpperLeft = 30;
		int relativeXofUpperRight = 21;
		int relativeYofUpperTop = 12;
		int relativeYofUpperBottom = 17;
		
		// this "Lower" means the lower small rectangle
		int relativeXofLowerLeft = 30;
		int relativeXofLowerRight = 21;
		int relativeYofLowerTop = 22;
		int relativeYofLowerBottom = 27;
		
		int threshold = 200;
		if (width < threshold) {
			relativeXofLeft = relativeXofLeft * width / threshold;
			relativeXofRight = relativeXofRight * width / threshold;
			relativeYofTop = relativeYofTop * width / threshold;
			relativeYofBottom = relativeYofBottom * width / threshold;
			
			// this Upper means the upper small rectangle
			relativeXofUpperLeft = relativeXofUpperLeft * width / threshold;
			relativeXofUpperRight = relativeXofUpperRight * width / threshold;
			relativeYofUpperTop = relativeYofUpperTop * width / threshold;
			relativeYofUpperBottom = relativeYofUpperBottom * width / threshold;
			
			// this "Lower" means the lower small rectangle
			relativeXofLowerLeft = relativeXofLowerLeft * width / threshold;
			relativeXofLowerRight = relativeXofLowerRight * width / threshold;
			relativeYofLowerTop = relativeYofLowerTop * width / threshold;
			relativeYofLowerBottom = relativeYofLowerBottom * width / threshold;
		}
		
		Point upLeft = new Point(relativeX-relativeXofLeft, relativeY+relativeYofTop);
		Point upRight = new Point(relativeX-relativeXofRight, relativeY+relativeYofTop);
		Point downLeft = new Point(relativeX-relativeXofLeft, relativeY+relativeYofBottom);
		Point downRight = new Point(relativeX-relativeXofRight, relativeY+relativeYofBottom);
		
		Point upperUL = new Point(relativeX-relativeXofUpperLeft, relativeY+relativeYofUpperTop);
		Point upperUR = new Point(relativeX-relativeXofUpperRight, relativeY+relativeYofUpperTop);
		Point upperDL = new Point(relativeX-relativeXofUpperLeft, relativeY+relativeYofUpperBottom);
		Point upperDR = new Point(relativeX-relativeXofUpperRight, relativeY+relativeYofUpperBottom);
		Point upperUM = new Point(relativeX-relativeXofLeft, relativeY+relativeYofUpperTop);
		Point upperDM = new Point(relativeX-relativeXofLeft, relativeY+relativeYofUpperBottom);
		
		Point lowerUL = new Point(relativeX-relativeXofLowerLeft, relativeY+relativeYofLowerTop);
		Point lowerUR = new Point(relativeX-relativeXofLowerRight, relativeY+relativeYofLowerTop);
		Point lowerDL = new Point(relativeX-relativeXofLowerLeft, relativeY+relativeYofLowerBottom);
		Point lowerDR = new Point(relativeX-relativeXofLowerRight, relativeY+relativeYofLowerBottom);
		Point lowerUM = new Point(relativeX-relativeXofLeft, relativeY+relativeYofLowerTop);
		Point lowerDM = new Point(relativeX-relativeXofLeft, relativeY+relativeYofLowerBottom);
		
		Point left = new Point(figureArea.x, figureArea.y+35);
		Point right = new Point(figureArea.x+width, figureArea.y+35);
		
//		Point upLeft = new Point(relativeX-25, relativeY+7);
//		Point upRight = new Point(relativeX-7, relativeY+7);
//		Point downLeft = new Point(relativeX-25, relativeY+32);
//		Point downRight = new Point(relativeX-7, relativeY+32);
//		
//		Point upperUL = new Point(relativeX-30, relativeY+12);
//		Point upperUR = new Point(relativeX-21, relativeY+12);
//		Point upperDL = new Point(relativeX-30, relativeY+17);
//		Point upperDR = new Point(relativeX-21, relativeY+17);
//		Point upperUM = new Point(relativeX-25, relativeY+12);
//		Point upperDM = new Point(relativeX-25, relativeY+17);
//		
//		Point lowerUL = new Point(relativeX-30, relativeY+22);
//		Point lowerUR = new Point(relativeX-21, relativeY+22);
//		Point lowerDL = new Point(relativeX-30, relativeY+27);
//		Point lowerDR = new Point(relativeX-21, relativeY+27);
//		Point lowerUM = new Point(relativeX-25, relativeY+22);
//		Point lowerDM = new Point(relativeX-25, relativeY+27);
//		
//		Point left = new Point(figureArea.x, figureArea.y+35);
//		Point right = new Point(figureArea.x+width, figureArea.y+35);
		
		graphics.drawLine(upLeft, upRight);
		graphics.drawLine(upRight, downRight);
		graphics.drawLine(downLeft, downRight);
		
		graphics.drawLine(upLeft, upperUM);
		graphics.drawLine(upperDM, lowerUM);
		graphics.drawLine(lowerDM, downLeft);
		
		graphics.drawLine(upperUL, upperUR);
		graphics.drawLine(upperUR, upperDR);
		graphics.drawLine(upperDR, upperDL);
		graphics.drawLine(upperDL, upperUL);
		
		graphics.drawLine(lowerUL, lowerUR);
		graphics.drawLine(lowerUR, lowerDR);
		graphics.drawLine(lowerDR, lowerDL);
		graphics.drawLine(lowerDL, lowerUL);
		
		graphics.drawLine(left, right);
		
		graphics.popState();
	}
	
	public TextFigure getComponentLable() {
		return componentLable;
	}
	
	public void setBounds(Rectangle rect) {
		super.setBounds(rect);
		if (root != null)
			root.setSize(rect.width, rect.height);
		if (body != null){
			body.setSize(rect.width, rect.height);
		}
	}
}
